<?php
header('Content-Type: Text/html; charset=utf-8');
require_once('config.inc.php');//$pdo; $uid; ROOT
require_once "uploadfile.class.php";

//$uid = 1;
if($uid <= 0){
	header('Refresh:3,Url=index.php');
	$result = ["success"=>"0","data"=>'你还未登陆!!!!请登陆再来'];
	die(json_encode($result));
}

//简单判断$_POST数据是否合法(存在就行)
if(empty($_POST['username']) || empty($_POST['cardid']) || empty($_POST['addr']) || empty($_POST['mobile']) || empty($_POST['huname']) || empty($_POST['huname1']) || empty($_POST['hmobile']) || empty($_POST['clazz'])){
	//header('Refresh:3,Url=index.php');
	$result = ["success"=>"0","data"=>'000非法操作!!!'];
	die(json_encode($result));
}

//查询下看有没有原信息,有就填充上去
$sql = "SELECT * FROM stu_info WHERE uid=?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$uid]);
//$my_data_rows = $stmt->rowCount();//获取查询结果行数
$my_data_all = $stmt->fetchAll(PDO::FETCH_ASSOC);
$my_data_rows = count($my_data_all);


//调用实例==========================================================
$upload = new UploadFile();
//if($upload->set('types',['jpg','jpeg','jpe','gif','png'])) echo '设置成功!<br />';
$upload->set('types',['jpg','jpeg','jpe','gif','png']);
$upload->set('up_dir','./uploads/temp');
$upload->set('max_size',5242880);//1048576，2097152，3145728，4194304，5242880，6291456

if($upload->upload()){
	//echo '上传成功';
	/*foreach($upload->get_files() as $v){
		//得到文件列表	
		echo '<img src="'.$v.'" />';
	}*/
	
	$result = ["success"=>"1","data"=>$_POST,"files"=>$upload->get_files()];
	
	if($my_data_rows > 0){
		$result['db_methon'] = 'update';
		$sql = 'UPDATE stu_info SET cid=?,uname=?,cardid=?,addr=?,tel=?,card_p=?,card_p1=?,h_name=?,h_call=?,h_tel=?,h1_name=?,h1_call=?,h1_tel=? WHERE uid = ?';
		$stmt = $pdo->prepare($sql);
		$db_after_rows = $stmt->execute([$_POST['clazz'],$_POST['username'],$_POST['cardid'],$_POST['addr'],$_POST['mobile'],$result['files'][0],$result['files'][1],$_POST['huname'],$_POST['huname1'],$_POST['hmobile'],$_POST['huname_1'],$_POST['huname1_1'],$_POST['hmobile_1'],$uid]);
		//如果新图片上传成功,则删除之前上传的旧图片
		foreach($my_data_all as $my_data){
			if(trim($my_data['card_p'])) @unlink(trim($my_data['card_p']));
			if(trim($my_data['card_p1'])) @unlink(trim($my_data['card_p1']));
		}
	}else{
		$result['db_methon'] = 'insert';	
		$sql = 'INSERT INTO stu_info(uid,cid,uname,cardid,addr,tel,card_p,card_p1,h_name,h_call,h_tel,h1_name,h1_call,h1_tel) VALUE(?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
		$stmt = $pdo->prepare($sql);
		$db_after_rows = $stmt->execute([$uid,$_POST['clazz'],$_POST['username'],$_POST['cardid'],$_POST['addr'],$_POST['mobile'],$result['files'][0],$result['files'][1],$_POST['huname'],$_POST['huname1'],$_POST['hmobile'],$_POST['huname_1'],$_POST['huname1_1'],$_POST['hmobile_1']]);
	}
}else{
	//echo  "上传身份证文件时出错,详情:".$upload->get_message();
	$result = ["success"=>"0","data"=>$upload->get_message()];
	if($my_data_rows > 0){
		$result['db_methon'] = 'update';
		$sql = 'UPDATE stu_info SET cid=?,uname=?,cardid=?,addr=?,tel=?,h_name=?,h_call=?,h_tel=?,h1_name=?,h1_call=?,h1_tel=? WHERE uid = ?';
		$stmt = $pdo->prepare($sql);
		$db_after_rows = $stmt->execute([$_POST['clazz'],$_POST['username'],$_POST['cardid'],$_POST['addr'],$_POST['mobile'],$_POST['huname'],$_POST['huname1'],$_POST['hmobile'],$_POST['huname_1'],$_POST['huname1_1'],$_POST['hmobile_1'],$uid]);
		//删除之前上传的图片
}else{
		$result['db_methon'] = 'insert';
		$sql = 'INSERT INTO stu_info(uid,cid,uname,cardid,addr,tel,h_name,h_call,h_tel,h1_name,h1_call,h1_tel) VALUE(?,?,?,?,?,?,?,?,?,?,?,?)';
		$stmt = $pdo->prepare($sql);
		$db_after_rows = $stmt->execute([$uid,$_POST['clazz'],$_POST['username'],$_POST['cardid'],$_POST['addr'],$_POST['mobile'],$_POST['huname'],$_POST['huname1'],$_POST['hmobile'],$_POST['huname_1'],$_POST['huname1_1'],$_POST['hmobile_1']]);
	}
}
$result['db_after_rows'] = (string)$db_after_rows;
file_put_contents('upload.txt', json_encode($result));
echo json_encode($result);


?>